package com.nali.dal.statement.function.formatter.date;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.nali.dal.statement.function.AbstractDataHandler;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionException;

/**
 * DateDivide<br>&nbsp;
 * 日期相减，默认分钟
 * @author sam Created on 2011-4-16
 */
@Component
public class DateDecrease extends AbstractDataHandler<Number> {
	@Override
	public void validate(List<FunctionArgument> arguments) throws FunctionException {
		checkSize(arguments, 2, 3);
	}
	
	@Override
	public Number handle(List<Object> argList) throws FunctionException {
		Date date1 = (Date) argList.get(0);
		Date date2 = (Date) argList.get(1);
		TimeUnit timeUnit;
		if(argList.size() > 2) {
			String timeUnitName = (String) argList.get(2);
			timeUnit = TimeUnit.getTimeUnit(timeUnitName);
		} else {
			timeUnit = TimeUnit.SECOND;
		}
		return timeUnit.between(date1, date2);
	}
}
